layui.use(["layer","form","jquery"],function () {
    var layer = layui.layer,form = layui.form,$ = layui.jquery;

    var commentDate = "";//当前时间日期
    //获取到微圈的数据的数据
    var commentMicrocircle = JSON.parse(localStorage.getItem("commentMicrocircle"));
    var commodityId = commentMicrocircle.microcircleId;

    //用户赋值
    var user = localStorage.getItem('user');
    user = JSON.parse(user);
    $('input[name=userName]').val(user.data.trueName);
    $('input[name=account]').val(user.data.account);

    //查询文章对应评论量 评论赋值
    //评论量
    $.ajax({
        url: "/microcircle/getMicrocircleCommentNum",
        data: {"commodityId": commodityId,"type":4},
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $("#acticleReviews").text(data.data);
            //修改评论数量
        }
    });

    var userType = localStorage.getItem("userType");
    //根据微圈查看对应的评论 type:4 微圈  userType：2 后台用户 1前端用户
    getAllComment();
    function getAllComment() {
        $.ajax({
            url: '/microcircle/getMicrocircleComment',
            type: 'post',
            dataType: 'json',
            data: {'commodityId': commodityId,"type": 4 ,"userType" : userType},
            success: function (data) {
                var list = data.data;
                var i = 0;
                for (i; i < list.length; i++) {
                    if (list[i].display == 2 && list[i].status == 1) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + list[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + list[i].showName + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + list[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + list[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment(' + list[i].id + ',' + list[i].display + ');">隐藏</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteTable(' + list[i].id + ');">删除</a>' + '</div>');
                    } else if (list[i].display == 3 && list[i].status == 1) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + list[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + list[i].showName + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + list[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + list[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment(' + list[i].id + ',' + list[i].display + ');">显示</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteTable(' + list[i].id + ');">删除</a>' + '</div>');
                    } else if (list[i].status == 3) {
                        $('.all_body').append('<div id="all_item">' + '<img id="photo" src="' + list[i].imageUrl + '"/>&nbsp;' +
                            '<label id="comment_user">' + list[i].showName + '：' + '</label>&nbsp;' +
                            '<label id="comment_content">' + list[i].content + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">' + list[i].commentDate + '</label>&nbsp;&nbsp;&nbsp;' +
                            '<label style="color: #037EFE">已删除</label>' + '</div>');
                    }
                }
            }
        })
    }

    power();
    function  power() {
        var userId = user.data.userId;
        var account = user.data.account;
        var utype = 0;
        $.ajax({
            url: '/comment/getDeptId',
            type: 'post',
            dataType: 'json',
            data: {"userId":userId ,"account":account},
            success: function (data) {
                if(data.data != null){
                    console.log("后台用户");
                    utype = 2;
                    localStorage.setItem("uType",utype);
                }else{
                    console.log("前端用户");
                    utype = 1;
                    localStorage.setItem("uType",utype);
                }
            }
        })
    }

    //提交评论
    form.on('submit(search)', function (obj) {
        var utp = localStorage.getItem("uType");
        getTime();
        var json = {
            userId: user.data.userId,
            commodityId: commodityId,
            type: 4,
            content: obj.field.content,
            parentId: 0,
            commentDate: commentDate,
            display: 2,
            status : 1,
            userType: utp,
        };
        $.ajax({
            url: '/microcircle/addMicrocircleComment',
            type: 'post',
            dataType: 'json',
            data: json,
            success: function (data) {
                location.reload();
            }
        })
    })

    //隐藏显示评论
    hideComment = function hideComment(id, display) {
        var dis = 0;
        if (display == 2) {
            dis = 3
        } else if (display == 3) {
            dis = 2
        }
        ;
        $.ajax({
            url: '/microcircle/editDisplay',
            type: 'post',
            dataType: 'json',
            data: {'id': id, 'display': dis},
            success: function (data) {
                location.reload();
            }
        })
    }

    //删除评论
    deleteTable = function deleteTable(id) {
        layer.confirm('确定删除此条评论吗?', {icon: 3, title:'删除'}, function(index){
            $.ajax({
                url: '/microcircle/deleteComment',
                type: 'post',
                dataType: 'json',
                data: {'id': id},
                success: function (data) {
                    if (data.data) {
                        location.reload();
                    }
                }
            })
        });
    }

    //现在时间
    function getTime() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        var dateStr = year + "-" + zeroFilling(month) + "-" + zeroFilling(day) + " " + zeroFilling(hour) + ":" + zeroFilling(minute) + ":" + zeroFilling(second);
        commentDate = dateStr;
    }
    function zeroFilling(data) {
        if (data < 10) {
            data = "0" + data;
        }
        return data;
    }

})